package org.pandaria.war3.w3g.analyseur;

import org.pandaria.war3.log.LogUtil;
import org.pandaria.war3.util.BitsUtil;

/**
 * For WarCraft III patch version >= 1.07:
 *   If bit 6 of player race flags is additionally set (0x40 added) then the
 *   race is selectable by the player - otherwise it is a ladder game or the
 *   race is fixed by the map (see also section 4.10).
 */
public class SlotRecord {
	
	/**
	 * 4.11 [SlotRecord]
	 */
	public byte[] bytes_slotRecord;
	
	/**
	 * Taille d'un enregistrement Slot Record
	 */
	public static int SLOT_RECORD_SIZE = 9;
	
	/**
	 * 0x0000 |  1 byte   | player id (0x00 for computer players)
	 */
	public byte[] bytes_playerID = new byte[1];
	
	/**
	 * 0x0001 |  1 byte   | map download percent: 0x64 in custom, 0xff in ladder
	 */
	public byte[] bytes_mapDownloadPercent = new byte[1];
	
	/**
	 * 0x0002 |  1 byte   | slotstatus:
     *  |           |   0x00 empty slot
     *  |           |   0x01 closed slot
     *  |           |   0x02 used slot
	 */
	public byte[] bytes_slotStatus = new byte[1];
	
	/**
	 * 0x0003 |  1 byte   | computer player flag:
     *  |           |   0x00 for human player
     *  |           |   0x01 for computer player
	 */
	public byte[] bytes_computerPlayerFlag = new byte[1];
	
	/**
	 * 0x0004 |  1 byte   | team number:0 - 11
     *  |           | (team 12 == observer or referee)
	 */
	public byte[] bytes_teamNumber = new byte[1];
	
	/**
	 * 0x0005 |  1 byte   | color (0-11):
     *  |           |   value+1 matches player colors in world editor:
     *  |           |   (red, blue, cyan, purple, yellow, orange, green,
     *  |           |    pink, gray, light blue, dark green, brown)
     *  |           |   color 12 == observer or referee
	 */
	public byte[] bytes_playerColor = new byte[1];
	
	/**
	 * 0x0006 |  1 byte   | player race flags (as selected on map screen):
     *  |           |   0x01=human
     *  |           |   0x02=orc
     *  |           |   0x04=nightelf
     *  |           |   0x08=undead
     *  |           |   0x20=random
     *  |           |   0x40=race selectable/fixed (see notes below)
	 */
	public byte[] bytes_playerRaceFlags = new byte[1];
	
	/**
	 * 0x0007 |  1 byte   | computer AI strength: (only present in v1.03 or higher)
     *  |           |   0x00 for easy
     *  |           |   0x01 for normal
     *  |           |   0x02 for insane
     *  |           | for non-AI players this seems to be always 0x01
	 */
	public byte[] bytes_computerAIStrength = new byte[1];
	
	/**
	 * 0x0008 |  1 byte   | player handicap in percent (as displayed on startscreen)
     *  |           | valid values: 0x32, 0x3C, 0x46, 0x50, 0x5A, 0x64
     *  |           | (field only present in v1.07 or higher)
	 */
	public byte[] bytes_playerHandicap = new byte[1];
	
	
	public SlotRecord(byte[] bytes_slotRecord){
		this.bytes_slotRecord = bytes_slotRecord;
	}
	
	/**
	 * Analyse d'un enregistrement de slot
	 */
	public void parse(){
		int offset = 0;
		
		offset = BitsUtil.subArray(this.bytes_slotRecord, offset, this.bytes_playerID);
		LogUtil.getLogger().debug("playerID = "+BitsUtil.hexa(this.bytes_playerID));
		
		offset = BitsUtil.subArray(this.bytes_slotRecord, offset, this.bytes_mapDownloadPercent);
		LogUtil.getLogger().debug("mapDownloadPercent = "+BitsUtil.hexa(this.bytes_mapDownloadPercent));
		
		offset = BitsUtil.subArray(this.bytes_slotRecord, offset, this.bytes_slotStatus);
		LogUtil.getLogger().debug("slotStatus = "+BitsUtil.hexa(this.bytes_slotStatus));
		
		offset = BitsUtil.subArray(this.bytes_slotRecord, offset, this.bytes_computerPlayerFlag);
		LogUtil.getLogger().debug("computerPlayerFlag = "+BitsUtil.hexa(this.bytes_computerPlayerFlag));
		
		offset = BitsUtil.subArray(this.bytes_slotRecord, offset, this.bytes_teamNumber);
		LogUtil.getLogger().debug("teamNumber = "+BitsUtil.ubits8(this.bytes_teamNumber));
		
		offset = BitsUtil.subArray(this.bytes_slotRecord, offset, this.bytes_playerColor);
		LogUtil.getLogger().debug("playerColor = "+BitsUtil.ubits8(this.bytes_playerColor));
		
		offset = BitsUtil.subArray(this.bytes_slotRecord, offset, this.bytes_playerRaceFlags);
		LogUtil.getLogger().debug("playerRaceFlags = "+BitsUtil.hexa(this.bytes_playerRaceFlags));
		
		offset = BitsUtil.subArray(this.bytes_slotRecord, offset, this.bytes_computerAIStrength);
		LogUtil.getLogger().debug("computerAIStrength = "+BitsUtil.hexa(this.bytes_computerAIStrength));
		
		offset = BitsUtil.subArray(this.bytes_slotRecord, offset, this.bytes_playerHandicap);
		LogUtil.getLogger().debug("playerHandicap = "+BitsUtil.hexa(this.bytes_playerHandicap));
		
	}

}
